package com.data.core.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.pojo.Result;
import com.common.vo.DictDataVo;
import com.data.core.entry.DictData;
import com.data.core.service.DictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(value = "字典相关API")
@RestController
@RequestMapping("dict")
public class DictDataController {

    @Resource
    private DictDataService dictDataService;

    @ApiOperation(value = "保存字典",notes = "保存字典")
    @ApiImplicitParam(name = "flag", value = "字典保存or更新标识", required = true, dataType = "boolean")
    @PostMapping(value = "save")
    public Result<?> save(@RequestBody DictDataVo dictDataVo,
                          Boolean flag) throws Exception {
        Result result = new Result();
        result.OK(dictDataService.saveOrUpdateDictData(dictDataVo,flag));
        return result;
    }

    @ApiOperation(value = "查询字典信息",notes = "查询字典信息")
    @ApiImplicitParam(name = "id", value = "字典id", required = true, dataType = "Long")
    @PostMapping("find")
    public Result<?> findDictData(@RequestParam Long id) throws Exception {
        Result result = new Result();
        result.OK(dictDataService.getByDictDataId(id));
        return result;
    }

    @ApiOperation(value = "删除字典",notes = "删除字典")
    @ApiImplicitParam(name = "id", value = "字典id", required = true, dataType = "Long")
    @GetMapping("delete")
    public Result<?> delete(@RequestParam Long id) throws Exception {
        Result result = new Result();
        result.OK(dictDataService.delete(id));
        return result;
    }

    @ApiOperation(value = "字典列表",notes = "字典列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", paramType = "query",required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageSize",value = "条数",paramType = "query",required = true,dataType = "int"),
    })
    @PostMapping("page")
    public Result<?> DictDataPage(@RequestBody DictDataVo dictDataVo,
                                  @RequestParam(value = "pageNum") int pageNum,
                                  @RequestParam(value = "pageSize")  int pageSize) throws Exception {
        Result result = new Result();
        Page<DictData> page = new Page<>(pageNum,pageSize);
        result.OK(dictDataService.dictDataPage(page,dictDataVo));
        return result;
    }

    @ApiOperation(value = "查询字典列表",notes = "查询字典列表")
    @PostMapping("findDictDataList")
    public Result<?> findDictDataList(@RequestBody DictDataVo dictDataVo) throws Exception {
        Result result = new Result();
        result.OK(dictDataService.getDictDataList(dictDataVo));
        return result;
    }
}
